//
//  ZJMineThroughRecordTableViewCell.m
//  ZJControlsClass
//
//  Created by wandoufo on 2024/12/7.
//

#import "ZJMineThroughRecordTableViewCell.h"
#import "Masonry.h"

@interface ZJMineThroughRecordTableViewCell()


@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UILabel *statusLabel;
@property (nonatomic, strong) UILabel *itemsLabel;
@property (nonatomic, strong) UILabel *areaLabel;
@property (nonatomic, strong) UIButton *actionButton;
@property (nonatomic, strong) UIImageView *arrowImageView;
@property (nonatomic, strong) UIView *separatorLine;
@property (nonatomic, strong) UIButton *touchArea;

@end

@implementation ZJMineThroughRecordTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.contentView.backgroundColor = [UIColor clearColor];
    self.backgroundColor = [UIColor clearColor];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    self.bgView =  [[UIView alloc] init];
    self.bgView.backgroundColor = [UIColor whiteColor];
    self.bgView.layer.masksToBounds = true;
    self.bgView.layer.cornerRadius = 8;
    [self.contentView addSubview:self.bgView];
    
    
    // 添加时间标签
    self.timeLabel = [[UILabel alloc] init];
    self.timeLabel.font = [UIFont systemFontOfSize:16];
    [self.contentView addSubview:self.timeLabel];
    
    // 添加状态标签
    self.statusLabel = [[UILabel alloc] init];
    self.statusLabel.font = [UIFont systemFontOfSize:14];
    self.statusLabel.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:self.statusLabel];
    
    // 添加物品标签
    self.itemsLabel = [[UILabel alloc] init];
    self.itemsLabel.font = [UIFont systemFontOfSize:14];
    self.itemsLabel.textColor = [UIColor grayColor];
    [self.contentView addSubview:self.itemsLabel];
    
    // 添加区域标签
    self.areaLabel = [[UILabel alloc] init];
    self.areaLabel.font = [UIFont systemFontOfSize:14];
    self.areaLabel.textColor = [UIColor grayColor];
    [self.contentView addSubview:self.areaLabel];
    
    // 添加分割线
    self.separatorLine = [[UIView alloc] init];
    self.separatorLine.backgroundColor = [UIColor lightGrayColor];
    [self.contentView addSubview:self.separatorLine];
    
    // 添加操作按钮
    self.actionButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.actionButton setTitle:@"查看详情" forState:UIControlStateNormal];
    self.actionButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:self.actionButton];
    
    // 添加箭头图标
    self.arrowImageView = [[UIImageView alloc] init];
    self.arrowImageView.image = [UIImage imageNamed:@"arrow_right"]; // 确保有这个图片资源
    [self.contentView addSubview:self.arrowImageView];
    
    // 添加点击区域
    self.touchArea = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView insertSubview:self.touchArea belowSubview:self.actionButton];
    [self.touchArea addTarget:self action:@selector(touchAreaTapped) forControlEvents:UIControlEventTouchUpInside];
    
    // Masonry布局
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(12);
        make.top.equalTo(self.contentView).offset(10);
        make.right.equalTo(self.contentView).offset(-12);
        make.bottom.equalTo(self.contentView);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).offset(16);
        make.top.equalTo(self.bgView).offset(10);
        make.right.lessThanOrEqualTo(self.statusLabel.mas_left).offset(-10);
    }];
    
    [self.statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.bgView).offset(-10);
        make.centerY.equalTo(self.timeLabel);
        make.width.mas_equalTo(65);
    }];
    
    [self.itemsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.timeLabel);
        make.top.equalTo(self.timeLabel.mas_bottom).offset(14);
        make.right.equalTo(self.bgView).offset(-10);
    }];
    
    [self.areaLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.timeLabel);
        make.top.equalTo(self.itemsLabel.mas_bottom).offset(4);
        make.right.equalTo(self.bgView).offset(-10);
    }];
    
    // 添加分割线约束
    [self.separatorLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).offset(15);
        make.right.equalTo(self.bgView).offset(-15);
        make.top.equalTo(self.areaLabel.mas_bottom).offset(12);
        make.height.mas_equalTo(0.5);
    }];
    
    // 添加按钮约束
    [self.actionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).offset(15);
        make.top.equalTo(self.separatorLine.mas_bottom).offset(12);
        make.bottom.equalTo(self.bgView).offset(-12);
        make.height.mas_equalTo(20);
    }];
    
    // 添加箭头约束
    [self.arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.bgView).offset(-15);
        make.centerY.equalTo(self.actionButton);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    // 添加点击区域约束
    [self.touchArea mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.bgView);
        make.top.equalTo(self.separatorLine.mas_bottom);
        make.bottom.equalTo(self.bgView);
    }];
}

- (void)updateWithRecord:(ZJMineThroughRecordModel *)record {
    self.timeLabel.text = record.checkTime;
    
    if ([record.status boolValue] == YES){
        self.statusLabel.text = @"正常";
        self.statusLabel.textColor = [UIColor blueColor];
    }else{
        self.statusLabel.text = @"异常";
        self.statusLabel.textColor = [UIColor redColor];
    }
    
    
    self.itemsLabel.text = [NSString stringWithFormat:@"携带物品：%@", record.goodsTypeName?record.goodsTypeName : @""];
    self.areaLabel.text = [NSString stringWithFormat:@"通行区域：%@", record.areaName];
    
    [self.contentView setNeedsLayout];
    [self.contentView layoutIfNeeded];
}

// 添加点击事件处理方法
- (void)touchAreaTapped {
    // 处理点击事件
//    if (self.actionBlock) {
//        self.actionBlock();
//    }
}

//// 添加点击回调属性
//@property (nonatomic, copy) void (^actionBlock)(void);
//
//// 添加设置点击回调的方法
//- (void)setActionBlock:(void (^)(void))block {
//    _actionBlock = block;
//}

@end
